<?php

namespace Admin\Controller;

class MessageboxController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Messagebox");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();
        $this->assign("begin_time", $_REQUEST["mb#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["mb#create_time-elt"]);
        $this->assign("product_id", $_REQUEST["mb#product_id"]);

        //分页
        $count = $this->currentModel->getListCount($requestMap);

        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取messagebox列表
        $messageboxList = $this->currentModel->queryForList($page, $requestMap);

        foreach( $messageboxList as &$item) {
            $category = D("Category")->queryById($item["category_id"]);
            $map["customer_id"] = $item["customer_id"];
            $map["product_id"] = $item["product_id"];
            if($category["is_finished"] == C("DEFAULT_YN.YES")) {
                $item["count"] = D("Order")->where($map)->count();
            }else{
                $item["count"] = D("Booking")->where($map)->count();
            }
        }

        $this->assign("defaultEnum", C("DEFAULT_YN"));
        $this->assign("statusEnum", C("MESSAGEBOX_STATUS"));
        $this->assign('messageboxList', $messageboxList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function viewOrder() {

        //分页
        //$count = $this->currentModel->getListCount($requestMap);
        //$page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取order列表
        $category = D("Category")->queryById($_REQUEST["category_id"]);

        if($category["is_finished"] == C("DEFAULT_YN.YES")) {
            $map["o.customer_id"] = $_REQUEST["customer_id"];
            $map["o.product_id"] = $_REQUEST["product_id"];
            $orderList = D("Order")->queryForList(null, $map);
        }else{
            $map["b.status"] = 1;
            $map["b.customer_id"] = $_REQUEST["customer_id"];
            $map["b.product_id"] = $_REQUEST["product_id"];
            $orderList = D("Booking")->queryForList(null, $map);
        }
        $this->assign("category", $category);
        $this->assign("defaultEnum", C("DEFAULT_YN"));
        $this->assign('bookingTypeEnum', C("BOOKING_TYPE"));
        $this->assign("statusEnum", C("ORDER_STATUS"));
        $this->assign('orderList', $orderList);
        $this->display();

    }

    public function viewNotify() {
        $messagebox = $this->currentModel->queryById($_REQUEST["id"]);
        $messagebox["name"] = $_REQUEST["name"];
        $messagebox["doctor"] = $_REQUEST["doctor"];
        $messagebox["hospital"] = $_REQUEST["hospital"];
        $messagebox["product"] = $_REQUEST["product"];
        $messagebox["count"] = $_REQUEST["count"];
        $this->assign("messagebox", $messagebox);
        $this->display();

    }

    public function notify()
    {

        $id = $_REQUEST["id"];

        $messagebox = $this->currentModel->queryById($id);
        $messagebox["status"] = C("MESSAGEBOX_STATUS.END");


        $company = D("Company")->queryById($_REQUEST["companyId"]);
        $customer = D("Customer")->queryById($company["customer_id"]);
        if($messagebox["notified"] != "")
            $messagebox["notified"] = $messagebox["notified"].",";
        $messagebox["notified"] = $messagebox["notified"].$customer["nickname"];

        //推送留言信息到销售
        $users_obj= M("admin");
        $id=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$id")->find();
        $notifyData["openid"] = $customer["openid"];
        $notifyData["first"] = "收到患者留言，请您联系该患者。——"."管理员【".$user["admin_name"]."】";
        $notifyData["keyword1"] = $_REQUEST["name"]."，城市【".$_REQUEST["city"]."】";
        $notifyData["keyword2"] = $_REQUEST["number"];
        $notifyData["keyword3"] = $_REQUEST["time"];
        $notifyData["keyword4"] = $_REQUEST["messageContent"];
        $notifyData["remark"] = "咨询产品：【".$_REQUEST["product"]."】，曾购买 ".$_REQUEST["count"]."件，所属医院：".$_REQUEST["hospital"]."，主治医生：".$_REQUEST["doctor"]."。";
        messageboxNotify($notifyData);
        $this->currentModel->updateInfo($messagebox);

        $this->success("已通知销售：", U("messagebox/index"));


    }

}